<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Tag
 *
 * @author QuangKhoi
 */
class Tag {
    //put your code here
    public $tagId;
    public $tagName;
    
    public function __constructor() {
        
    }
    
    public static function getList() {
        $db = Database::getInstance();
        $userId = $_SESSION["userid"];
        $result = $db->query("SELECT * FROM tags WHERE userid = '$userId' OR userid = '0'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $tags = array();
                foreach($result->dataTable->rows as $row) {
                    $tag = new self();
                    $tag->tagId = $row["tagid"];
                    $tag->tagName = $row["tagname"];
                    $tags[] = $tag;
                }
                return $tags;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any stream with provided id");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
    
    public static function withId($tagId) {
        $instance = new self();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM tags WHERE tagid = '$tagId'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $instance->tagId = $row["tagid"];
                $instance->tagName = $row["tagname"];
                return $instance;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any tag with provided id");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
    
    public static function getTag($messageId) {
        $db = Database::getInstance();
        $userId = $_SESSION["userid"];
        $result = $db->query("SELECT tagid FROM tagsdata JOIN signals ON tagsdata.signalid = signals.signalid WHERE (tagsdata.userid = '$userId' OR tagsdata.userid = '0') AND messageid='$messageId'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $tags = array();
                foreach($result->dataTable->rows as $row) {
                    $tag = self::withId($row["tagid"]);
                    $tags[] = $tag;
                }
                return $tags;
            }
            else {
                return array();
            }
        }
        return array();
    }
    
    public static function deleteTag($tagid) {
        $db = Database::getInstance();
        $query = "DELETE FROM tags WHERE tagid = '$tagid'"; 
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return "true";
        }
        else {
            return "false";
        }
    }
    
    public function add($signalId) {
        $userId = $_SESSION["userid"];
        $query = "SELECT * FROM tagsdata WHERE tagid = '$this->tagId' AND signalid='$signalId'";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow == 0) {
                Database::getInstance()->query("INSERT INTO tagsdata 
                            (userid, tagid, signalid) VALUES
                            ('$userId', '$this->tagId',
                            '$signalId')");
                return $this;
            }
        return NULL;
    }
}

?>
